import Vue from 'vue'
import VueRouter from 'vue-router'

import {getToken,getTokenUser} from "../utils/auth";
import Home from "../views/User/Home";

//user component
import User from "../views/User/User";
import Login from "../views/User/Login"
import ChangeUserPass from "../components/User/ChangeUserPass";
import AllProject from "../components/User/AllProject";
import DelProject from "../components/User/DelProject";
import AddProject from "../components/User/AddProject";
import ProjectId from "../components/User/ProjectId";

//admin component
import Admin from "../views/Admin/Admin";
import AdminLogin from "../views/Admin/Login";
import AdminHome from "../views/Admin/Home"
import AdminWelcome from "../components/Admin/Welcome"
import Alluser from "../components/Admin/Alluser";
import Adduser from "../components/Admin/Adduser";
import Changepass from "../components/Admin/Changepass";
import Deluser from "../components/Admin/Deluser";
import ChangeAdminPass from "../components/Admin/ChangeAdminPass";
import ResetAdmin from "../components/Admin/ResetAdmin";
import LookLog from "../components/Admin/LookLog";

Vue.use(VueRouter);

const routes = [
  {
    path: '/',
    name: 'User',
    component: User,
    redirect: "/user/login",
    children:[{
        path:"user",
        component:User,
        children: [
          {
            path: "login",
            component: Login,
            name:"Login"
          },
          {
            path: "home",
            component: Home,
            children:[
              {
                path:"changepass",
                component:ChangeUserPass
              },
              {
                path: "allproject",
                component: AllProject
              },
              {
                path:"delmyproject",
                component:DelProject
              },
              {
                path:"new",
                component:AddProject
              },
              {
                path:"allproject/:id",
                component:ProjectId
              }
            ]
          },
        ]
    }]

  },
    //admin
  {
    path:'/admin',
    name:'Admin',
    redirect:'/admin/login',
    component: Admin,
    children:[
      {
        path:'login',
        component:AdminLogin,
        name:'AdminLogin'
      },
      {
        path:'home',
        component: AdminHome,
        name: "AdminHome",
        redirect:'/admin/home/welcome',
        children:[
          {
            path:"welcome",
            component: AdminWelcome
          },
          {
            path:"alluser",
            component: Alluser
          },
          {
            path:"adduser",
            component:Adduser
          },
          {
            path:"upuserpass",
            component:Changepass
          },
          {
            path:"deluser",
            component:Deluser
          },
          {
            path:"changepass",
            component:ChangeAdminPass
          },
          {
            path:"resetadmin",
            component:ResetAdmin
          },
          {
            path:"viewlog",
            component:LookLog
          }
        ]
      }
    ]
  }
];

const router = new VueRouter({
  mode: 'hash',
  base: '/',
  routes
});

router.beforeEach((to,from,next) => {
  if(to.path.indexOf("/admin") !== -1 && !getToken() && to.name !== "AdminLogin") next({name:'AdminLogin'});
  next()
});

router.beforeEach((to,from,next) => {
  if(to.path.indexOf("/user") !== -1 && !getTokenUser() && to.name !== "Login") next({name:'Login'});
  next()
});

export default router
